package com.test;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 *  level 0: policy
 *  level 1 global 
 *  level2...  local 
 * @author xiandafu
 *
 */
public class ScopeStack {
	
	Stack< Scope> stack = new Stack< Scope>();
	public void push( Scope o){
		stack.push(o);
	}
	
	public  Scope pop(){
		return stack.pop();
	}
	
	public Scope peek(){
		return stack.peek();
	}
	
	public boolean isEmpty(){
		return stack.isEmpty();
	}
	/**
	 * 直接指定的优先返回，其次是较近的action，最后是全局的
	 * @return
	 */
	public Iterator ruleIteator(){
		//stack reurn new Iterator(){ ...}
		/**
		 * 
		 */
		int size = stack.size();
		List<RuleNode> list = new LinkedList<RuleNode>();
	
		//this
		Scope s = null;
	
		for(int i = size-1;i>=1;i--){
			s = stack.get(i);
			
			if(i==size-1){
				if( s.forThis!=null){
					list.addAll(s.forThis);
				}
			}
			
			List all = s.forAll;
			if(i==1){
				if(all!=null)list.addAll(0,all);
			}else{
				if(all!=null)list.addAll(all);
			}
			
			if(i==0){
				//global
				if( s.forThis!=null){
					list.addAll(s.forThis);
				}
			}
		}
		return list.iterator();
		
		
		
	}
	
	
}
